<?php
if (!defined('ZZQSS')){
	die("Access Denied");
}
/**
 * groupclass類
 * 
 * add()添加
 * edit()編輯
 * del()刪除
 * getdata()獲取數據數組
 * buynum 獲取修改購買數量
 *
 */
class GroupClass
{
	var $db;
	var $table;
	var $tablepre;
	function __construct()
	{
		//設置全局變量
		global $db,$tablepre;
		$this->db=$db;
		$this->tablepre=$tablepre;
		$this->table=$tablepre."groups";
		
	}
	function GroupClass()
	{
		$this->__construct();
	}
	//添加團購信息 傳遞數組
	function add($garray)
	{
		if(is_array($garray))
		{
			if(!empty($garray['title']) && !empty($garray['sn']) && !empty($garray['market_price']) && !empty($garray['shop_price']) && !empty($garray['stock']) && !empty($garray['max_buynum']) && !empty($garray['group_begin_time']) && !empty($garray['group_end_time']))
			{
				//插入數據庫操作	
				$gid=inserttable($this->table, $garray,1);
				if(is_numeric($gid)){
					if(!empty($garray['goods_pics'])){
						include_once(INC_PATH.'/attach.class.php');
						$att=new AttachClass();
						$att->setgid($gid,$garray['goods_pics']);
					}
					return "OK";
				}
				//$sql="insert into {$this->table}(typeid,sid,title,sn,cityid,gtypeid,freefreight,order,weight,score,market_price,shop_price,earnest_money,stock,max_buynum,group_usernum,group_state,group_bond_end_time,group_begin_time,group_end_time,goods_pics,goods_reviews,goods_reviews_contents,goods_reviews_site,goods_brief,goods_about,seokeyword,seocontent) VALUES(0,{$garray[sid]},'{$garray[title]}','{$garray[sn]}',0,'{$garray[gtypeid]}','{$garray[freefreight]}','{$garray[order]}','{$garray[weight]}','{$garray[score]}','{$garray[market_price]}','{$garray[shop_price]}','{$garray[earnest_money]}','{$garray[stock]}','{$garray[max_buynum]}','{$garray[group_usernum]}','{$garray[group_state]}','{$garray[group_bond_end_time]}','{$garray[group_begin_time]}','{$garray[group_end_time]}','{$garray[goods_pics]}','{$garray[goods_reviews]}','{$garray[goods_reviews_contents]}','{$garray[goods_reviews_site]}','{$garray[goods_brief]}','{$garray[goods_about]}','{$garray[seokeyword]}','{$garray[seocontent]}')";
				//$this->db->query($sql);
				
			}else {
				return "缺少字段";
			}
			
		}else 
		{
			return "數據格式錯誤";
		}
			
	}
	//編輯團購信息 傳遞數組
	function edit($gid,$garray)
	{
		if(is_array($garray) && !empty($gid)){
			if(!empty($garray['title']) && !empty($garray['sn']) && !empty($garray['market_price']) && !empty($garray['shop_price']) && !empty($garray['stock']) && !empty($garray['max_buynum']) && !empty($garray['group_begin_time']) && !empty($garray['group_end_time']))
			{
				//更新數據庫操作	
				$wheresqlarr=" gid='{$gid}'";
				updatetable($this->table,$garray, $wheresqlarr);
				if(!empty($garray['goods_pics'])){
					include_once(INC_PATH.'/attach.class.php');
					$att=new AttachClass();
					$att->setgid($gid,$garray['goods_pics']);
				}
				//$sql="update {$this->table} set sid='{$garray[sid]}',title='{$garray[title]}',sn='{$garray[sn]}',gtypeid='{$garray[gtypeid]}',freefreight='{$garray[freefreight]}',order='{$garray[order]}',weight='{$garray[weight]}',score='{$garray[score]}',market_price='{$garray[market_price]}',shop_price='{$garray[shop_price]}',earnest_money='{$garray[earnest_money]}',stock='{$garray[stock]}',max_buynum='{$garray[max_buynum]}',group_usernum='{$garray[group_usernum]}',group_state='{$garray[group_state]}',group_bond_end_time={$garray[group_end_time]},group_begin_time={$garray[group_begin_time]},group_end_time={$garray[group_end_time]},goods_pics='{$garray[goods_pics]}',goods_reviews='{$garray[goods_reviews]}',goods_reviews_contents='{$garray[goods_reviews_contents]}',goods_reviews_site='{$garray[goods_reviews_site]}',goods_brief='{$garray[goods_brief]}',goods_about='{$garray[goods_about]}',seokeyword='{$garray[seokeyword]}',seocontent='{$garray[seocontent]}'";
				//$this->db->query($sql);
				return "OK";

			}else {
				return "缺少參數";
			}
		}else {
			return "數據格式錯誤";
		}
	
		
	}
	//刪除指定團購信息
	function del($gid,$r="")
	{
		if(is_numeric($gid)&& $r=="")
		{
			$this->db->query("delete from {$this->table} where gid='{$gid}'");
			$this->db->query("delete from {$this->tablepre}groupbond where gid='{$gid}'");
			include_once(INC_PATH.'/attach.class.php');
			$att=new AttachClass();
			$att->delgid($gid);//刪除該gid相關圖片
			
			return true;
		}elseif (is_numeric($gid)&& $r=="r"){
			$this->db->query("update `$this->table` SET state=0 WHERE gid='{$gid}'");
			return true;
		}else {
			return false;			
		}

	}
	
	//取得一組團購資訊
	//add by squall.f@gmail.com
	
	function getOneGroup($gid,$type="ALL"){
		if (GetNum($gid)) {
			
			$groupid = $this->getdata("","","gid=".GetNum($gid),"","title");
			//echo "[".."]";
			$gpdata=$this->getdata("",$type,"state=1 AND (title = '".$groupid[0]['title']."')");
			//$gpdata=$this->getdata("",$type,"gid='{$gid}'");
			$value=$gpdata;					
		}

		if ($type=="ALL") {
			//獲取團購相關圖片
			include_once(INC_PATH.'/attach.class.php');
			$att=new AttachClass();						
			
			if(!empty($value['goods_pics'])){
			$imgarray=$att->getdata("","aid in({$value['goods_pics']})");
			}
			if(is_array($imgarray)){
				foreach ($imgarray as $valueimg){
					$echoimg[$valueimg[aid]]=$valueimg;
				}
			}else $echoimg=array();
			$value['imgarray']=$echoimg;
		
			
		}
		//數據整理部分
		$value['goods_reviews_contents']=unserialize($value['goods_reviews_contents']);
		
		
		return $value;
	}	
		
	//獲取一條團購信息
	function getone($gid,$type="ALL"){
		if (GetNum($gid)) {
			
			$gpdata=$this->getdata(1,$type,"gid='{$gid}'");
			$value=$gpdata[0];
		}
		if ($type=="ALL") {
			//獲取團購相關圖片
			include_once(INC_PATH.'/attach.class.php');
			$att=new AttachClass();
			if(!empty($value['goods_pics'])){
			$imgarray=$att->getdata("","aid in({$value['goods_pics']})");
			}
			if(is_array($imgarray)){
				foreach ($imgarray as $valueimg){
					$echoimg[$valueimg[aid]]=$valueimg;
				}
			}else $echoimg=array();
			$value['imgarray']=$echoimg;
		}
		//數據整理部分
		$value['goods_reviews_contents']=unserialize($value['goods_reviews_contents']);
		return $value;
	}	
	
	
	
	/**
	 * 獲取團購信息列表
	 *
	 * @param  $limit
	 * @param  $where
	 * @param  $orderby
	 * 
	 */

	function getdata($limit="",$type="ALL",$where="",$orderby="",$field="")
	{
		$tempdata=array();
		if(!empty($limit))$limit=" limit $limit ";
		if(!empty($where))$where=" where $where ";
		if(empty($field))$field=" * ";
		if(!empty($orderby))$orderby=" order by $orderby ";elseif ($type=="ALLIMG") $orderby=" order by G.gid desc";else $orderby=" order by gid desc";
		if($type=="ALL")$addsql=" AS G left join {$this->tablepre}suppliers AS S ON G.sid=S.sid ";
		elseif($type=="ALLIMG")$addsql=" AS G left join {$this->tablepre}suppliers AS S ON G.sid=S.sid left join {$this->tablepre}attachment AS A ON G.goods_pic=A.aid ";
		elseif($type=="IMG")$addsql=" AS G left join {$this->tablepre}attachment AS A ON G.goods_pic=A.aid ";
		else $addsql="";
		$sql="select {$field} from {$this->table}{$addsql}{$where}{$orderby}{$limit}";
		//echo $sql."<Br>";
		$query =$this->db->query($sql);
		$i=0;
		while($value = $this->db->fetch_array($query)) {
			//此處可進行數據整理
			//價格格式化
			//統計所有的購買數量
			$total_sql = "SELECT  title, SUM( group_buynum ) as total_num FROM {$this->tablepre}groups WHERE title='".$value['title']."' GROUP BY title";
			$total_value = $this->db->fetch_array($this->db->query($total_sql));
			//echo $sql;
			$value['total_buynum'] = $total_value['total_num'];
			$value['market_price']=round($value['market_price'],2);
			$value['shop_price']=round($value['shop_price'],2);
			$value['earnest_money']=round($value['earnest_money'],2);
			$value['id']=$i;
			$tempdata[]=$value;
			$i++;
		}

		return $tempdata;
	}
	function getcount($where=""){
		if(!empty($where))$where=" where $where";
		$count= $this->db->result_first("Select count(*) From `$this->table` $where");
		return $count;
	
	}
	/**
	 * 獲取更改已購買數量
	 *
	 * @param int $gid
	 * @param int $num
	 * @return int/true
	 */
	function buynum($gid=0,$num=0)
	{
		$timestamp=time();
		
		if (GetNum($gid)) {
			$row = $this->db->fetch_first("Select group_buynum,group_usernum,group_success_time,stock From `$this->table` where gid = '{$gid}'");
			if(is_array($row)){
				if($gid>0 && $num==0){
					return $row['group_buynum'];
				}elseif ($gid>0 && $num!=0){
					$this->db->query("update `$this->table` SET group_buynum = group_buynum + {$num} WHERE gid='{$gid}'");
					
					if (($row['group_buynum']+$num)>=$row['group_usernum'] && empty($row['group_success_time'])) {
						//購買數量達到最低購買數量
						editstate($this->table,"group_success_time","gid='{$gid}'",$timestamp); //修改團購成功時間
						editstate($this->table,"group_state","gid='{$gid}'",2); //修改團購成功狀態
					}elseif (($row['group_buynum']+$num)>=$row['stock']){
						editstate($this->table,"group_state","gid='{$gid}'",3); //修改團購賣光了狀態
					}
					return true;
				}else return false;
			
			
			}
		}
	}
	
	
	
	
}


?>
